package pl.edu.mimuw.irs.rcp.controller.handler;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;

import pl.edu.mimuw.irs.core.model.design.IRSWorkflow;
import pl.edu.mimuw.irs.core.util.persistence.IPersistenceManager;
import pl.edu.mimuw.irs.core.util.persistence.PersistenceAdapter;
import pl.edu.mimuw.irs.rcp.controller.editor.IRSWorkflowEditor;
import pl.edu.mimuw.irs.rcp.util.Logger;
import pl.edu.mimuw.irs.rcp.util.MessageCode;

/**
 * @author Jakub Rauch
 * Created on: 2009-05-25
 */
public class NewWorkflowHandler extends AbstractHandler {

	// *** Attributes

	// *** Methods
	
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		
		IRSWorkflow workflow = new IRSWorkflow();
		workflow.createSampleModel();
		IPersistenceManager manager = PersistenceAdapter.getManager(workflow.getClass());
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		
		try {
			manager.writeToStream(workflow, baos);
		} catch (Exception ex) {
			Logger.handle(MessageCode.WORKFLOW_SAVE_SERIALIZE_PROBLEM, ex);
			return null;
		}
		
		InputStream source = new ByteArrayInputStream(baos.toByteArray());
		IWorkspaceRoot rootWorkspace = ResourcesPlugin.getWorkspace().getRoot();
		
		IProject project = rootWorkspace.getProject("newProject10");
		IFile file = null;
		
		try {
		    // The null parameters are going to be
		    // replaced with IProgressMonitors eventually
			project.create(null);
			project.open(null);
		} catch (CoreException e1) {  
			Logger.handle(MessageCode.WORKFLOW_CREATE_PROJECT_PROBLEM, e1);
			return null;
		}
		
		try {
		    file = project.getFile("workflow.irs");
		    file.create(source, IResource.FORCE, null);
		} catch (CoreException e1) {
			Logger.handle(MessageCode.WORKFLOW_SAVE_FILE_PROBLEM, e1);
			return null;
		}
		        
		FileEditorInput fileEditorInput = new FileEditorInput(file);
		IWorkbench workbench = PlatformUI.getWorkbench();
		//IEditorDescriptor desc = workbench.getEditorRegistry().getDefaultEditor(file.getName());
		IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage();
		//page.openEditor(fileEditorInput, desc.getId());
		try {
			page.openEditor(fileEditorInput, IRSWorkflowEditor.ID);
		} catch (PartInitException e) {
			Logger.handle(MessageCode.EDITOR_OPEN_PROBLEM, e, fileEditorInput.getName());
			return null;
		}
		 
		return null;
	}
}
